package cn.yunhe.servlet;

import cn.yunhe.FenYe;
import cn.yunhe.entity.LiuYan;
import cn.yunhe.service.LiuYanService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class LiuYanListServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断当前页
        String pageI = request.getParameter("pageIndex");
        if (pageI == null) {
            pageI = "1";
        }

        int pageIndex = Integer.parseInt(pageI);

        LiuYanService liuYanDao = new LiuYanService();
        //获取总条数
        int count = liuYanDao.allCount();
        //获取总页数
        int pageSize = FenYe.pageSize(count, FenYe.pageNum);
        //控制当前页范围
        if (pageIndex < 1) {
            pageIndex = 1;
        }

        if (pageIndex > pageSize) {
            pageIndex = pageSize;
        }

        //返回当前页的集合
        List<LiuYan> liuYanList = liuYanDao.getAll(FenYe.pageNum, pageIndex);

        request.setAttribute("liuYanList",liuYanList);
        request.setAttribute("pageIndex",pageIndex);
        request.setAttribute("pageSize",pageSize);

        request.getRequestDispatcher("HuiFuServlet").forward(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
